package view.diaLogView;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.text.R;
import app.MyApplication;
import data.SharedPreferencesUtils;
import util.AgentIp;
import util.HomeUtil;
import util.JumpActivityUtils;
import util.NetWorkstateUtil;
import util.Url;

/**
 * Created by liuxiaobin on 2017/4/19
 * 非会员和余额不足的提示
 */
public class MemberDialog extends Dialog{
    private MemberDialog mMemberDialog;
    private Context mContext;
    private TextView mTextView;
    private int mType; //1 是提示加入会员  2提示余额充值


    public MemberDialog(Context context,int type) {
        super(context, R.style.upoad_dialog);
        this.mContext = context;
        this.mType = type;
        mMemberDialog = this;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = inflater.inflate(R.layout.dialog_member, null);
        TextView textViewTitle = (TextView)mView.findViewById(R.id.dialog_member_tv_title);
        TextView textViewExplain = (TextView)mView.findViewById(R.id.dialog_member_tv_explain);
        TextView textViewNo = (TextView)mView.findViewById(R.id.dialog_member_tv_no);
        mTextView = (TextView)mView.findViewById(R.id.dialog_member_timeR);
        TextView textViewOk = (TextView)mView.findViewById(R.id.dialog_member_tv_ok);
        ImageView imageView = (ImageView)mView.findViewById(R.id.dialog_member_iv);
        LinearLayout linearLayout1 = (LinearLayout)mView.findViewById(R.id.dialog_member_ll1);
        LinearLayout linearLayout2 = (LinearLayout)mView.findViewById(R.id.dialog_member_ll2);
        if(mType == 1){
            textViewTitle.setText("缴纳保证金成为会员即可上网。");
            textViewExplain.setText("会员保证金可退还！");
            textViewExplain.setVisibility(View.VISIBLE);
            linearLayout1.setVisibility(View.VISIBLE);
            linearLayout2.setVisibility(View.GONE);
        }else if(mType == 2){
            textViewTitle.setText("你的时长不足，暂时不能连接网络。");
            textViewTitle.setPadding(0,40,0,0);
            textViewExplain.setVisibility(View.GONE);
            linearLayout1.setVisibility(View.GONE);
            linearLayout2.setVisibility(View.VISIBLE);
        }
        textViewNo.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {mMemberDialog.dismiss();}});

        textViewOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = (String) SharedPreferencesUtils.getParam(mContext, HomeUtil.mUseridDB,"");
                String wevVip = (int) SharedPreferencesUtils.getParam(mContext, HomeUtil.mVIPCombonum,1)+"";
                JumpActivityUtils.getInstance().jumpWebView(mContext, Url.mATIAIRIP + Url.getUrlNew().vipdePosit+"?userid="+id+"&vipid="+wevVip+"&pattern="+ MyApplication.getApp().getmPattern(),"会员购买");
                mMemberDialog.dismiss();
            }});

        linearLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mTextView.getText().equals("连接中")){return;}
                mTextView.setText("连接中...");
                AgentIp.newInstance(mContext).setNetWorkstate2(new AgentIp.OnNetworkStateClickListener() {
                    @Override
                    public void onClick(boolean state) {
                        if(state){
                            Url.getUrlNew().setNETWORKING_ATATE(0);
                            JumpActivityUtils.getInstance().jumpTimeRecharge(mContext,"");mMemberDialog.dismiss();
                        }else {
                            NetWorkstateUtil.getInstance().showNetworking(mContext, new NetWorkstateUtil.NetworkingOnClickListener() {
                                @Override
                                public void onItemClick(boolean state) {
                                    if(state){
                                        JumpActivityUtils.getInstance().jumpTimeRecharge(mContext,"");mMemberDialog.dismiss();
                                    }else {
                                        mTextView.setText("时长充值");
                                        Toast.makeText(mContext,"连接互联网失败,可以连接移动网络充值",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    }
                });

            }});

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {mMemberDialog.dismiss();}
        });
        setContentView(mView);
    }
}
